package com.jambo.yudada.manager;

import com.jambo.yudada.common.ErrorCode;
import com.jambo.yudada.exception.BusinessException;
import com.volcengine.ark.runtime.model.completion.chat.*;
import com.volcengine.ark.runtime.service.ArkService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Flowable;
/**
 * 通用 AI 调用能力
 */
@Component
public class DeepSeekManager {

    @Resource
    private ArkService arkService;

    // 稳定的随机数
    private static final double STABLE_TEMPERATURE = 0.05;

    // 不稳定的随机数
    private static final double UNSTABLE_TEMPERATURE = 0.99;

    /**
     * 同步请求（答案不稳定）
     *
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSyncUnstableRequest(String systemMessage, String userMessage) {
        return doRequest(systemMessage, userMessage, Boolean.FALSE, UNSTABLE_TEMPERATURE);
    }

    /**
     * 同步请求（答案较稳定）
     *
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSyncStableRequest(String systemMessage, String userMessage) {
        return doRequest(systemMessage, userMessage, Boolean.FALSE, STABLE_TEMPERATURE);
    }

    /**
     * 同步请求
     *
     * @param systemMessage
     * @param userMessage
     * @param temperature
     * @return
     */
    public String doSyncRequest(String systemMessage, String userMessage, Double temperature) {
        return doRequest(systemMessage, userMessage, Boolean.FALSE, temperature);
    }

    /**
     * 通用请求（简化消息传递）
     *
     * @param systemMessage
     * @param userMessage
     * @param stream
     * @param temperature
     * @return
     */
    public String doRequest(String systemMessage, String userMessage, Boolean stream, Double temperature) {
        List<ChatMessage> messages = new ArrayList<>();
        ChatMessage systemChatMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content(systemMessage).build();
        messages.add(systemChatMessage);
        ChatMessage userChatMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(userMessage).build();
        messages.add(userChatMessage);
        return doRequest(messages, stream, temperature);
    }

    /**
     * 通用请求
     *
     * @param messages
     * @param stream
     * @param temperature
     * @return
     */
    public String doRequest(List<ChatMessage> messages, Boolean stream, Double temperature) {
        // 构建请求
            ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
                    .model("deepseek-r1-250528")
                    .stream(stream)
                    .messages(messages)
                    .temperature(temperature)
                    .build();
try {
    ChatCompletionResult chatCompletion = arkService.createChatCompletion(streamChatCompletionRequest);
    return chatCompletion.getChoices().get(0).toString();
}catch (Exception e){
    e.printStackTrace();
    throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
}


    }

    /**
     * 通用流式请求（简化消息传递）
     *
     * @param systemMessage
     * @param userMessage
     * @param temperature
     * @return
     */
    public Flowable<ChatCompletionChunk> doStreamRequest(String systemMessage, String userMessage, Float temperature) {
        List<ChatMessage> chatMessageList = new ArrayList<>();
        ChatMessage systemChatMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content(systemMessage).build();
        chatMessageList.add(systemChatMessage);
        ChatMessage userChatMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(userMessage).build();
        chatMessageList.add(userChatMessage);
        return doStreamRequest(chatMessageList, temperature);
    }
    /**
     * 通用流式请求
     *
     * @param messages
     * @param temperature
     * @return
     */
    public Flowable<ChatCompletionChunk> doStreamRequest(List<ChatMessage> messages, Float temperature) {
        // 构建请求
        ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
                .model("deepseek-r1-250528")
                .messages(messages)
                .stream(Boolean.TRUE)
                .temperature(0.05)
                .build();
        try {
            Flowable<ChatCompletionChunk> chatCompletionChunkFlowable = arkService.streamChatCompletion(streamChatCompletionRequest);
            return chatCompletionChunkFlowable;
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }
}
